/**
 * 模型加载器
 */
const fs = require('fs');
const path = require("path");

const load = (dir, cb) => {
  const url = path.resolve(__dirname, dir)
  const files = fs.readdirSync(url);
  files.forEach(filename => {
    filename = filename.replace('.js', '');
    const file = require(url + '/' + filename)
    cb(filename, file)
  })
}

const loadModel = config => app => {
  const conn = mongoose.connection
  conn.on('error', () => console.error('数据库连接识别'));

  app.$model = {}
  loadModel('../model', (filename, {
    scheme
  }) => {
    console.log('load model:' + filename, scheme);
    app.$model[filename] = mongoose.model(filename, scheme)
  })
}